﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Stores.Interface;
using Microhard.Plat.CRM.Stores.DataAccess;
using Microhard.Plat.CRM.Stores.DTO;
using Microhard.Plat.CRM.Stores.Msg;
using Microhard.Plat.CRM.Stores.Models;


namespace Microhard.Plat.CRM.Stores.Impls
{
    public class KAImpl : IKA
    {
        public bool IsDuplicate(Models.KAModel model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var cnt = db.KAs.Count(n => n.Name == model.Name);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.KAModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            try
            {
                //获得当前产品关联的company
                op.CompanyID = this.scope.EmpInfo.CompanyID;
                db.KAs.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, KAMsg.AddFailed);
                Log.Error("IKA.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.KAModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.KAs.SingleOrDefault(l => l.KAID == model.KAID);
            if (entity != null)
            {
                entity.Address = model.Address;
                entity.Cellphone = model.Cellphone;
                entity.Contractor = model.Contractor;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, KAMsg.UpdateFailed);
                Log.Error("IKA.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.KAs.Where(l => ids.Contains(l.KAID));
            foreach (var e in entity)
            {
                //是否在使用
                if (e.SubKAs.Count == 0)
                {
                    db.KAs.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(KAMsg.IsInUsing, e.Name));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, KAMsg.DeleteFailed);
                    Log.Error("ICompany.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.KAModel model)
        {
            throw new NotImplementedException();
        }

        public Models.KAModel GetModelByID(Guid id)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            return db.KAs.SingleOrDefault(c => c.KAID == id).ToModel();
        }

        public Models.KAModel GetModelByModel(Models.KAModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.KAModel> Query(Models.KAModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            IEnumerable<KA> entities = from b in db.KAs
                                       where b.CompanyID == this.scope.EmpInfo.CompanyID
                                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.Name).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<KAModel>();
        }

        public List<Models.KAModel> Query(Models.KAModel Model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            return (from k in db.KAs
                    where this.scope.PKID.Contains(k.KAID)
                    select k.ToModel()).ToList();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }


        public List<KAModel> GetKAByCompanyID(Guid companyID)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            return (from k in db.KAs
                    where k.CompanyID == companyID
                    select k).ToList().Select(t => t.ToModel()).ToList();
        }


        public bool IsCustomeCodeDuplicate(KAModel model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var cnt = db.KAs.Count(n => n.CustomerCode == model.CustomerCode);
            return cnt > 0 ? true : false;
        }
    }
}
